package thread;

/**
 * 线程创建
 * 1、继承Thread类，重写run方法
 * 2、实现Runnable接口，
 * 3、创建匿名内部类
 * 启动程序----调用start() gfcxgcvhcvhvc
 * 可用sleep()设置休眠减缓速度
 *
 */
public class ThreadCreat extends Thread{
    private String name;

    public ThreadCreat(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        while (true){
            System.out.println("我是线程"+name);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    public static void main(String[] agrs){
        ThreadCreat tc1=new ThreadCreat("线程1");
        ThreadCreat tc2=new ThreadCreat("线程2");
        tc1.start();tc2.start();//启动线程
        MyRun mr1=new MyRun();//实例化一个实现接口类的对象
        MyRun mr2=new MyRun();
        Thread th1=new Thread(mr1);//创建线程
        Thread th2=new Thread(mr2);
        th1.start();th2.start();
        //创建匿名内部类
        Thread th3=new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("我是通过匿名内部类创建的线程");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) { }
                }
            }
        });
        th3.start();
        while (true){
            System.out.println("我是主线程！");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) { }
        }
    }

}
class MyRun implements Runnable{//Runnable接口
    @Override//抽象函数的实现
    public void run() {
        while (true){
            System.out.println("我是线程"+Thread.currentThread().getName());
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) { }
        }
    }
}